package exp03.tracker.service;

import exp03.tracker.dao.CoronaVirusData;
import exp03.tracker.models.Chart;
import exp03.tracker.models.DateNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import exp03.tracker.models.Region;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther L_geng
 * @Date 2020/11/18
 */
@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private CoronaVirusData coronaVirusData;

    /*
    * 获取全部国家数据
    * */
    @Override
    public List<Region> getAllRegions(){
        return coronaVirusData.getAllRegionStats();
    }
    /*
    * 根据国家名称获取数据
    * */
    @Override
    public Map<String,Object> getByCountryName(String country){
        Map<String, Object> result = new HashMap<>();
        Map<String,Long> countryData = new LinkedHashMap<>();

        List<Region> one = new ArrayList<>();
        //查询国家数据
        one = coronaVirusData.getAllRegionStats()
                    .parallelStream()
                    .filter(locationStates -> locationStates.getCountry().equals(country))
                 .collect(Collectors.toList());
//        this.coronaVirusData.getAllRegionStats().forEach((e) -> {
//            if (e.getCountry().toLowerCase().contains(country.toLowerCase())) {
//                one.add(e);
//            }
//        });
        //病例数初始化为0
        for(String s : one.get(0).getData().keySet()){
            countryData.put(s,0L);
        }
        one.forEach((e)->{
            for(String s : e.getData().keySet()){
                //计算每天总量
                countryData.put(s,countryData.get(s)+e.getData().get(s));
            }
        });
        //计算折线图的行坐标
        List<DateNumber> rows = new ArrayList<>();
        for(String s : countryData.keySet()){
            rows.add(new DateNumber(s,countryData.get(s)));
        }
        Collections.reverse(rows);
        //添加折线图横纵坐标数据
        Chart chart = new Chart(Arrays.asList("date","cases"),rows);
        //获取最新数据日期
        String nowDate = (String) one.get(0).getData().keySet().toArray()[0];
        //全国总数
        Long totalNow = countryData.get(nowDate);

        Long addedNow = 0L;
        for (Region region : one){
            //新增总数
            addedNow += region.getDiffFromPrevDay();
        }
        result.put("name",country);         //国家名称
        result.put("totalNow",totalNow);   //目前病例总数
        result.put("addedNow", addedNow);    //目前新增病例
        result.put("nowDate", nowDate);      //目前统计时间
        result.put("totalData", one);      //各个地区情况
        result.put("chart",chart);//全球每天总数
        return result;
    }

    @Override
    public Map<String,Object> getAllData(){
        Map<String, Object> result = new HashMap<>();//返回结果集
        Map<String, Long> dataMap = new LinkedHashMap<>();//存储全球每天总病例数

        List<Region> allRegions = this.coronaVirusData.getAllRegionStats();
        //初始化每天总量为0
        for(String s:allRegions.get(0).getData().keySet()){
            dataMap.put(s,0L);
        }
        allRegions.forEach((e) -> {
            for (String s : e.getData().keySet()) {
                //全球每天总数求和
                dataMap.put(s,dataMap.get(s)+e.getData().get(s));
            }
        });
        //list转换
        List<Region> list = allRegions;
        //获取最新数据日期
        String nowDate = (String) allRegions.get(0).getData().keySet().toArray()[0];
        //全球总数
        Long totalNow = dataMap.get(nowDate);
        //新增总数
        Long addedNow = 0L;
        for (Region region : list) {
            addedNow += region.getDiffFromPrevDay();
        }
        //获取折线图 行 数据
        List<DateNumber> rows = new ArrayList<>();
        for(String s : dataMap.keySet()){
            rows.add(new DateNumber(s,dataMap.get(s)));
        }
        Collections.reverse(rows);
        //添加折线图 行和列 数据
        Chart chart = new Chart(Arrays.asList("date","cases"),rows);
        result.put("name","全球");//名称
        result.put("totalNow",totalNow);   //目前病例总数
        result.put("addedNow", addedNow);    //目前新增病例
        result.put("nowDate", nowDate);      //目前统计时间
        result.put("totalData", list);      //各个地区情况
        result.put("chart",chart);//全球每天总数
        return result;
    }
}
